import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;


public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择运算：Add/Sub");
        String opt = scanner.nextLine();
        Class<?> optClass = null;
        try {
            optClass = Class.forName(opt);
            Constructor constructor = optClass.getConstructor();
            Calculator calculator = (Calculator) constructor.newInstance();
            System.out.print("请输入第一个操作数：");
            double val1 = scanner.nextDouble();
            System.out.print("请输入第二个操作数：");
            double val2 = scanner.nextDouble();
            double ret = calculator.calculate(val1, val2);
            System.out.println(val1 + " " + opt + " " + val2 +" = " + ret);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
